package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{

    private CheckBox shuttlecock;
    private CheckBox baskefftball;
    private CheckBox pingpang;
    private TextView hobby;
    private String hobbys;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initEvent();
    }
    private void initView(){
        shuttlecock = findViewById(R.id.like_shuttlecock);
        baskefftball = findViewById(R.id.like_basketball);
        pingpang = findViewById(R.id.like_pingpang);
        hobby = findViewById(R.id.hobby);
    }
    private void initData(){
        //用于存放被选中的文本信息
        hobbys = new String();
    }
    private void initEvent(){
        shuttlecock.setOnCheckedChangeListener(this);
        baskefftball.setOnCheckedChangeListener(this);
        pingpang.setOnCheckedChangeListener(this);

        }
        public void onCheckedChanged(CompoundButton compoundButton,boolean b) {
            String motion = compoundButton.getText().toString();//获取被点击的控件的文本信息
            if (b){
                if (!hobbys.contains(motion)){    //判断当前选中的文本信息是否已经存在hobbys
                    hobbys = hobbys + motion;
                    hobby.setText(hobbys);
                }
            }else {
                if (hobbys.contains(motion)){
                    hobbys = hobbys.replace(motion,"");
                    hobby.setText(hobbys);
                }

            }
        }

    }

